package com.zlp.config;

import com.aliyun.oss.OSSClient;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties(OSSProperties.class)
public class OSSConfiguration
{
	@Resource(name = "ossProperties")
	private OSSProperties ossProperties;
	
	@Bean("ossClient")
	public OSSClient ossClient() {
		@SuppressWarnings("deprecation")
        OSSClient client = new OSSClient(
				ossProperties.getEndpoint(), 
				ossProperties.getAccessKeyId(), 
				ossProperties.getAccessKeySecret());
		return client;
	}

	@Bean
	@ConditionalOnBean(OSSClient.class)
	@ConditionalOnMissingBean(OSSTemplate.class)
	public OSSTemplate ossTemplate(OSSClient ossClient) {
		return new OSSTemplate(ossClient);
	}
}
